# 从前序遍历和中序遍历构建一颗二叉树
class TreeNode:

    def __init__(self, val, left, right):
        self.val = val
        self.left = left
        self.right = right

class Solution:

    def buildTree(self, preorder, inorder):

        if len(inorder) == 0:
            return None
        
        # 找到根节点
        root = TreeNode(preorder[0])
        mid = inorder.index(preorder[0])

        root.left = self.buildTree(preorder=preorder[1: mid + 1], inorder=inorder[0: mid])
        root.right = self.buildTree(preorder=preorder[mid + 1:], inorder= inorder[mid + 1: ])

        return root
    


